import { Component, EventEmitter, OnInit, Output } from "@angular/core";
import { NzMessageService } from "ng-zorro-antd/message";
import { NzTreeNode } from "ng-zorro-antd/tree";
import { AccessItemService } from "./access-item.service";

@Component({
  selector: "app-access-item",
  templateUrl: "./access-item.component.html",
  styles: []
})
export class AccessItemComponent implements OnInit {

  constructor(private msgSrv: NzMessageService,
              private accessItemService: AccessItemService
  ) {
  }

  nzLoading = false;

  @Output() voted = new EventEmitter();

  visible = false;

  checked = false;
  indeterminate = false;

  listOfCurrentPageData = [];
  setOfCheckedId = new Set<number>();

  loading = false;

  // 列表数据
  dataList = [];

  // 查询参数
  searchBeans = [{
    attributeName: "sysAccessClass.id",
    searchType: "EQ",
    attributeValue: ""
  }];
  // 分页参数
  pageData = { page: 0, size: 10, total: 100 };

  open(): void {
    this.visible = true;
    this.loadListData();
  }

  edit(param) {
    this.visible = true;
    this.searchBeans[0].attributeValue = param.id;
    this.loadListData();
  }

  close(): void {
    this.visible = false;
  }

  getCaptcha(e: MouseEvent): void {
    e.preventDefault();
  }

  ngOnInit(): void {

  }

  nzDisplayWith(node: NzTreeNode) {
    return node.title;
  }

  onChange($event: string): void {
    console.log($event);
  }

  /**
   * 加载列表数据
   */
  loadListData() {

    this.loading = true;

    const searchData = {
      page: this.pageData.page,
      size: this.pageData.size,
      searchFilter: {
        searchBeans: this.searchBeans
      }
    };

    this.accessItemService.findPage(searchData).subscribe((res) => {

      this.loading = false;

      if (res.code === 1) {
        this.pageData.page = res.data.number;
        this.pageData.size = res.data.size;
        this.pageData.total = res.data.totalElements;
        this.dataList = res.data.content;
      } else {
        this.msgSrv.error(res.message);
      }

    });
  }
}
